package com.macro.mall.portal.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.macro.mall.mapper.BmsLeaveApprovalMapper;
import com.macro.mall.model.BmsLeaveApproval;
import com.macro.mall.model.BmsLeaveApprovalExample;
import com.macro.mall.model.UmsMember;
import com.macro.mall.portal.service.BmsLeaveApprovalService;
import com.macro.mall.portal.util.MemberUtils;
import com.macro.mall.portal.util.UserUtils;
import com.macro.mall.query.BmsLeaveApprovalQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class LeaveApprovalServiceImpl implements BmsLeaveApprovalService {

    @Resource
    BmsLeaveApprovalMapper leaveApprovalMapper;


    @Override
    public List<BmsLeaveApproval> findList(BmsLeaveApprovalQuery query) {
        Integer pageNum = query.getPageNum();
        Integer pageSize = query.getPageSize();
        if (Objects.nonNull(pageNum) && Objects.nonNull(pageSize)) {
            PageHelper.startPage(pageNum, pageSize);
        }

        BmsLeaveApprovalExample example = new BmsLeaveApprovalExample();
        BmsLeaveApprovalExample.Criteria criteria = example.createCriteria();
        if (Objects.nonNull(query.getLeaveType())) {
            criteria.andLeaveTypeEqualTo(Byte.valueOf(query.getLeaveType()));
        }
        return leaveApprovalMapper.selectByExample(example);
    }

    @Override
    public int create(BmsLeaveApprovalQuery query) {

        UmsMember user = MemberUtils.getUser();

        BmsLeaveApproval bmsLeaveApproval = new BmsLeaveApproval();
        BeanUtil.copyProperties(query, bmsLeaveApproval);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (ObjectUtil.isNotEmpty(query.getStartTime())) {

            try {
                Date date = sdf.parse(query.getStartTime());
                bmsLeaveApproval.setStartTime(date);
            } catch (ParseException e) {
                log.error("字符串日期转错误！");
            }
        }

        if (ObjectUtil.isNotEmpty(query.getEndTime())) {
            try {
                Date date = sdf.parse(query.getEndTime());
                bmsLeaveApproval.setEndTime(date);
            } catch (ParseException e) {
                log.error("字符串日期转错误！");
            }
        }

        bmsLeaveApproval.setUserId(user.getId());
        bmsLeaveApproval.setUserName(user.getUsername());
        bmsLeaveApproval.setCreateUser(user.getId());
        bmsLeaveApproval.setCreateTime(new Date());
        bmsLeaveApproval.setUpdateUser(user.getId());
        bmsLeaveApproval.setUpdateTime(new Date());
        return leaveApprovalMapper.insertSelective(bmsLeaveApproval);

    }
}
